package com.zhiqian.rpc;

import com.zhiqian.bean.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse msg) throws Exception {
        try {
            log.info("客户端接收到的消息为：{}",msg);
            AttributeKey<RpcResponse> rpcResponse = AttributeKey.valueOf("rpcResponse");
            ctx.channel().attr(rpcResponse).set(msg);
            ctx.channel().close();
        }finally {
            ReferenceCountUtil.release(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("处理过程调用时出现错误");
        cause.printStackTrace();
        ctx.close();
    }
}
